<?php

namespace Home\Controller;

/**
 * Description of BookingController.class控制器实例
 * 
 * @author paddy
 * @date 2016-1-4
 */
class AppController extends ParentController {

    /**
     * @description 客栈申请列表视图页面
     * @modify paddy 2016-01-04
     */
    public function index() {
        $param = I('get.');
        $pageIndex = empty($param['p']) ? 1 : intval($param['p']);
        $pageIndex = $pageIndex > 0 ? $pageIndex : 1;
        $hotelName = $param['name'];
        $pageSize = 10;        

        $hotelEntityModel = D('HotelEntity');
        $hotelEntityList = $hotelEntityModel->getList($pageIndex, $pageSize, $hotelName);
        $rowCount = $hotelEntityModel->getRows($hotelName);
        
        $hotelIds = array_column($hotelEntityList, 'id');
        $hotelIds = array_unique($hotelIds);        
        
        $hotelAppModel = D('HotelApp');
        $hotelAppList = $hotelAppModel->getList($hotelIds);
        $hotelAppList = getArrayValueAsIndex($hotelAppList, 'hotelentity_id', false);
        
        foreach ($hotelEntityList as $key => &$hotel){
            if(isset($hotelAppList[$hotel['id']])){
                $appInfo = $hotelAppList[$hotel['id']];
                $hotel['app_status'] = $appInfo['status'];
                $hotel['app_enddate'] = $appInfo['enddate'];
            }else {
                $hotel['app_status'] = 0;
                $hotel['app_enddate'] = '0000-00-00';
            }
            $hotel['app_dueStyle'] = time()>strtotime($hotel['app_enddate'])?'color:red':'color:green';
        }
        unset($hotel);
        
        $link = "index_###." . C('URL_HTML_SUFFIX');
        if(!empty($param['name'])){
            $paramList[] = 'name=' . $param['name'];
        }
        if(!empty($paramList)){
            $link .= '?' . implode('&', $paramList);
        }
        $pageUtil = classPage($rowCount, $pageSize, $link);
        $page = $pageUtil->show();
        $this->assign('hotelName', $hotelName);
        $this->assign('page', $page);
        $this->assign('hotelAppList', $hotelEntityList);
        $this->display();
    }
    public function edit() 
    {
        $hotelId = I('post.hotelId');
        $status = I('post.status');
        $endData = I('post.endData');        
        
        $hotelAppModel = D('HotelApp');
        $appInfo = $hotelAppModel->getInfo($hotelId);
        if(empty($appInfo)){
            $data = array();
            $data['hotelentity_id'] = $hotelId;
            $data['status'] = empty($status)?0:1;
            if(!empty($endData)){
                $data['enddate'] = $endData;
            }
            $hotelAppModel->add($data);
        }else {
            $data = array();
            $data['status'] = empty($status)?0:1;
            if(!empty($endData)){
                $data['enddate'] = $endData;
            }
            $hotelAppModel->where(array(
                'hotelentity_id' => $hotelId,
            ))->save($data);            
        }
        
        $this->returnJson(200, 200);
    }

}
